#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $field[6][8] = [ _
	["0",0,0,0,0x0,0x0,0x0,0x0], _
	["1",1,0,0,0x1B7201,0x114A00,0x74F524,0x4CA017], _
	["2",3,0,0,0xDAA100,0x8F6900,0xCC9902,0x856401], _
	["3",5,0,0,0x510003,0x350001,0x84000C,0x560007], _
	["4",0,5,0,0xE0CC97,0x938563,0xD9C38C,0x8E7F5B], _
	["5",0,0,1,0xA19986,0x696457,0x373530,0x24221F] _
]
Global $l_award = 2
Global $last_map

Func create_award()
	Local $award[11] = [ _
	0, _ ; 0 mana
	0, _ ; 1 damag
	0, _ ; 2 health
	0, _ ; 3 max L
	0, _ ; 4 affect
	0, _ ; 5 step
	0, _ ; 6 chance L >= 3
	0, _ ; 7 chance L >= 4
	0, _ ; 8 chance L >= 5
	0, _ ; 9 chance L =  6
	0 _  ; 10 forecast enemy
	]
	Return $award
EndFunc

Func create_map()
	Local $map[$row_length][$col_length]
	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1
			$map[$r][$c] = False
		Next
	Next
	Return $map
EndFunc

Func show_map(ByRef $map)
	$last_map = $map
	Local $cnt[UBound($field,1)]
	For $f = 0 To UBound($field,1) - 1
		$cnt[$f] = 0
	Next
	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1
			Local $i = $map[$r][$c]
			set_field($r,$c,$field[$i][0])
			set_field_state($r,$c,False)
			If $i >= 0 And $i < UBound($field,1) Then
				$cnt[$i] += 1
			EndIf
		Next
	Next
	For $v = 0 To $adv_max - 1
		set_advice($v,"")
	Next
	Local $str = ""
	For $f = 1 To UBound($field,1) - 1
		$str &= $f & " " & $cnt[$f] & @CRLF
	Next
	set_stat($str)
EndFunc

Func select_map(ByRef $map)
	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1
			set_field_state($r,$c,$map[$r][$c])
		Next
	Next
EndFunc

Func count_map(ByRef $map, ByRef $award, ByRef $log)
	Local $lines[1000]
	Local $lines_cnt = 0

	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1 - 2
			Local $v = $map[$r][$c]
			If $v > 0 And $map[$r][$c + 1] = $v  And $map[$r][$c + 2] = $v Then
				Local $l = 3
				Local $last = $c + 3
				For $i = $last To $col_length - 1
					If $map[$r][$i] = $v Then
						$l += 1
					Else
						$last = $i
						ExitLoop
					EndIf
				Next
				If $log Then $log &= "[" & $r & "," & $c & "] 0,1 v=" & $v & " l=" & $l & @CRLF
				Local $line[6] = [0,1,$r,$c,$v,$l]
				$lines[$lines_cnt] = $line
				$lines_cnt += 1
				$c = $last
			EndIf
		Next
	Next

	For $c = 0 To $col_length - 1
		For $r = 0 To $row_length - 1 - 2
			Local $v = $map[$r][$c]
			If $v > 0 And $map[$r + 1][$c] = $v  And $map[$r + 2][$c] = $v Then
				Local $l = 3
				Local $last = $r + 3
				For $i = $last To $row_length - 1
					If $map[$i][$c] = $v Then
						$l += 1
					Else
						$last = $i
						ExitLoop
					EndIf
				Next
				If $log Then $log &= "[" & $r & "," & $c & "] 1,0 v=" & $v & " l=" & $l & @CRLF
				Local $line[6] = [1,0,$r,$c,$v,$l]
				$lines[$lines_cnt] = $line
				$lines_cnt += 1
				$r = $last
			EndIf
		Next
	Next
	
	Local $affect = 0
	For $l = 0 To $lines_cnt - 1
		Local $line = $lines[$l]
		Local $r = $line[2]
		Local $c = $line[3]
		If $award[$l_award + 1] < $line[5] Then
			$award[$l_award + 1] = $line[5]
		EndIf
		For $n = 1 To $line[5]
			Local $v = $map[$r][$c]
			If $v > 0 Then
				For $a = 0 To $l_award
					$award[$a] += $field[$v][$a + 1]
				Next
				$map[$r][$c] = 0
				$affect += 1
			EndIf
			$r += $line[0]
			$c += $line[1]
		Next
	Next
	$award[$l_award + 2] += $affect
	If $affect > 0 Then
		$award[$l_award + 3] += 1
	EndIf
	
	Return $affect
EndFunc

Func trim_map(ByRef $map, ByRef $award, ByRef $log)
	Local $count = 0
	For $c = 0 To $col_length - 1
		Local $r = $row_length - 1
		While $r > 0
			If $map[$r][$c] = 0 Then
				Local $k = $r - 1
				While $k >= 0 And $map[$k][$c] = 0
					$k -= 1
				WEnd
				If $k >= 0 Then
					$map[$r][$c] = $map[$k][$c]
					$map[$k][$c] = 0
					$count += 1
				Else
					ExitLoop
				EndIf
			EndIf
			$r -= 1
		WEnd
	Next
	Return $count
EndFunc

Func calc_full(ByRef $map, ByRef $award, ByRef $log, $make_forecast = True)
	Local $step = 0
	If $log Then $log &= "begin " & $step & @CRLF & map_to_str($map) & @CRLF

	While True
		Local $affect = count_map($map,$award,$log)
		If $affect = 0 Then ExitLoop
		
		If $log Then $log &= "count " & $step & ": " & award_to_str($award) & @CRLF & map_to_str($map) & @CRLF
		
		Local $trim = trim_map($map, $award, $log)
		
		If $trim And $log Then $log &= "trim " & $step & ": " & $trim & @CRLF & map_to_str($map) & @CRLF

		If $make_forecast Then forecast_count_drop($map,$award,$log)
		
		$step += 1
	WEnd
	Return $step
Endfunc

